<?php
namespace app\we\controller;
use think\Controller;
use app\we\sdk\Thinkwechat;


class Pay extends Controller{

    public function _initialize()
    {
        $this->buy_url ="http://".$_SERVER['HTTP_HOST']. url('We/Pay/pay');
        $this->notify_url ="http://".$_SERVER['HTTP_HOST']. url('We/Pay/notify');
    }

    /**
     * 收银台
     * 支付类型 1.临时支付，2.订单支付
     * 支付方式 1.api 2.扫码支付
     * $this 498944516@qq.com
     */
    public function index(){
      
         dump(111);
        return $this->fetch();
    
    }

    /**
     * 零时直接支付
     * @param openid 谁，顾客，可能不是支付者本人
     * @param product 商品
     * @param um 数量
     * @param price 价格
     * @param url 自动回调
     * @param ext 回调参数
     * 
     * @author $this 498944516@qq.com
     */
    public function pay($appid='wxea877923bf435db8'){
         
        ini_set('date.timezone','Asia/Shanghai');

        $options = model('We')->info($appid);
     
        $data=[
        'buyer'=>'1111111',
        'product'=>'商品名称',
        'disc'=>'无描述',
        'um'=>2,
        'price'=>2,
        'url'=>url('paySuccess'),
        'ext'=>''
        ];

         //数据有效性
        if(!isset($data['buyer']) || !isset($data['um'])) $this->error('缺少支付参数');
         //全局引入微信支付类
        require_once(APP_PATH.'We/paysdk/example/WxPay.JsApiPay.php');
        //①、获取用户openid
        $tools = new \JsApiPay();
        $openId = $tools->GetOpenid();

        //②、统一下单
        $input = new \WxPayUnifiedOrder();

        $input->SetBody($data['product']);
        $input->SetAttach($data['product']);
         
        $input->SetOut_trade_no($out_trade_no=($options['mchid']?$options['mchid']:\WxPayConfig::MCHID).date("YmdHis"));
        
        $input->SetTotal_fee($data['price']*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag($data['disc']);
        $input->SetNotify_url( $this->notify_url);
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
      
      
        $order = \WxPayApi::unifiedOrder($input);
        // $this->printf_info($order);
        $jsApiParameters = $tools->GetJsApiParameters($order);
         
        //获取共享收货地址js函数参数
        $editAddress = $tools->GetEditAddressParameters();

        //写入支付记录，标记为未支付
        $data['out_trade_no']=$out_trade_no;
        // $this->addPay($data);

      
       $this->assign('jsApiParameters',$jsApiParameters);
       $this->assign('data',$data);
       $this->assign('appid',$appid);
       return $this->fetch('pay');


    }

    // 处理微信消息总入口
    public function messageType(){
       
        switch($this->type) {
            case Thinkwechat::MSGTYPE_TEXT:
                   $reply=model('rob/RobAnswer')->info($this->data['Content'],$this->data);
                    break;
            case Thinkwechat::MSGTYPE_IMAGE:
                   $reply=model('rob/RobAnswer')->info($this->type,$this->data,1);  //根据类型进准匹配
                    break;
            case Thinkwechat::MSGTYPE_VOICE:
                   //TODO::语音识别
                   $reply=model('rob/RobAnswer')->info($this->type,$this->data,1);
                    break;
            case Thinkwechat::MSGTYPE_MUSIC:
                   
                    break;
            
            case Thinkwechat::MSGTYPE_VIDEO:
                  
                    break;
            case Thinkwechat::MSGTYPE_LOCATION:
                  
                    break;
            case Thinkwechat::MSGTYPE_LINK:
                  
                    break;

            case Thinkwechat::MSGTYPE_EVENT:
                  $reply=$this->messageEvent();
                    break;
            default: 
                 $reply=['type'=>'text','message'=>'success'];
                  break;
                    
        }

        return $reply;

    }

    // 处理微信事件消息
    public function messageEvent(){
       
        switch($this->data['Event']){
            case Thinkwechat::EVENT_SUBSCRIBE:
                //记录扫码获取机器人回答
                $res=db('WeFans')->where('openid',$this->openid)->update(['subscribe'=>1,'subscribe_time'=>time()]);
                $answer_id=model('WeSceneScan')->scan($this->data);
                $reply=model('rob/RobAnswer')->info($answer_id,$this->data,2);
                break;
            case Thinkwechat::EVENT_UNSUBSCRIBE:
                $res=db('WeFans')->where('openid',$this->openid)->update(['subscribe'=>0,'unsubscribe_time'=>time()]);
                $reply=['type'=>'text','message'=>'success'];
                break;
            case Thinkwechat::EVENT_SCAN:
                 //记录扫码获取机器人回答
                $answer_id=model('WeSceneScan')->scan($this->data);
                mylog($answer_id);
                $reply=model('rob/RobAnswer')->info($answer_id,$this->data,2);
                mylog($reply);
                 break;
            case Thinkwechat::EVENT_LOCATION:
               
                break;
            case Thinkwechat::EVENT_MENU_CLICK:

              
                break;
            case Thinkwechat::EVENT_MENU_SCAN_PUSH:
                 
                break;
            case Thinkwechat::EVENT_MENU_SCAN_WAITMSG:
               
                
                break;
            case Thinkwechat::EVENT_MENU_PIC_SYS:
                
                break;
            case Thinkwechat::EVENT_MENU_PIC_PHOTO:
               
                break;
            case Thinkwechat::EVENT_MENU_PIC_WEIXIN:
               
                break;
            case Thinkwechat::EVENT_MENU_LOCATION:
               
                break;
            case Thinkwechat::EVENT_SEND_MASS:
               
                break;
            case Thinkwechat::EVENT_SEND_TEMPLATE:
                //模板消息发送成功
                $map['ToUserName']=$this->data['FromUserName'];
                $map['FromUserName']='templateMessage';
                $message=db('WeMessage')->where($map)->order('CreateTime desc')->find();
                $res=db('WeMessage')->where(['id'=>$message['id']])->setField('status',1);
               
                break;
            case Thinkwechat::EVENT_KF_SEESION_CREATE:
                
                break;
            case Thinkwechat::EVENT_KF_SEESION_CLOSE:
                
                break;
            case Thinkwechat::EVENT_KF_SEESION_SWITCH:
                
                break;
            case Thinkwechat::EVENT_CARD_PASS:
                
                break;
            case Thinkwechat::EVENT_CARD_NOTPASS:
                
                break;
            case Thinkwechat::EVENT_CARD_USER_GET:
                
                break;
            case Thinkwechat::EVENT_CARD_USER_DEL:
                
                break;
            case Thinkwechat::WifiConnected :
                
                break;
            case Thinkwechat::ShakearoundUserShake :
                
                break;
            default:
                break;
                   
    
            }

            return $reply;
        }

  

}